package com.example.leetcode.DynamicProgramming;

/**
 * @Description TODO
 * @date 2021/6/30 9:15
 * @Author liuzhihui
 * @Version 1.0
 *
 *
 * dp[0] = nums[0]
 *
 * dp[n] = Math.max(dp[n-1],dp[n-1]+nums[n])
 *
 *
 */
public class Solution_6_53 {
    public static void main(String[] args) {

        int[] nums = new int[]{-2,1,-3,4,-1,2,1,-5,4};
        int i = maxSubArray(nums);
        System.out.println(i);
    }
    public static int maxSubArray(int[] nums) {
        int dp[] = new int[nums.length];
        dp[0] = nums[0];
        int max = 0;
        for (int i = 1; i < nums.length; i++) {
            dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
            if (max < dp[i]){
                max = dp[i];
            }
        }
        return max;
    }
}
